package com.sjq.controller;

import com.sjq.model.common.BaseResult;
import com.sjq.model.entity.Paper;
import com.sjq.model.entity.PaperBase;
import com.sjq.model.entity.Rating;
import com.sjq.model.request.ContentDto;
import com.sjq.model.request.PaperRequest;
import com.sjq.model.response.PaperByManagerResponse;
import com.sjq.model.response.PaperDetailResponse;
import com.sjq.service.IPaperService;
import com.sjq.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 稿件Controller
 *
 * @author sjq
 * @date 2022-03-13
 */
@Api(tags = "稿件")
@RestController
@RequestMapping("/sjq/paper")
public class PaperController
{
    @Autowired
    private IPaperService paperService;

    @Autowired
    private UserUtil userUtil;

    /**
     * 新增稿件
     */
    @RequiresRoles(value = {"专家","作者"},logical = Logical.OR)
    @ApiOperation("新增稿件")
    @PostMapping
    public BaseResult add(@RequestBody PaperBase paper, @RequestHeader String Authorization)
    {
        if(paperService.insertPaper(paper)>0)
            return BaseResult.success("发布成功");

        else
            return BaseResult.error();
    }

    /**
     * 修改状态
     */
    @RequiresAuthentication
    @ApiOperation("征用稿件")
    @GetMapping("edit/{ids}")
    public BaseResult edit(@PathVariable Long[] ids, @RequestHeader String Authorization)
    {
        if(paperService.updatePaperStatus(ids)>0)
            return BaseResult.success("修改成功");
        else
            return BaseResult.error();
    }

    /**
     * 专家评价稿件
     */
    @RequiresRoles(value = {"专家"})
    @ApiOperation("专家评价稿件")
    @PostMapping("/evaluate")
    public BaseResult evaluate(@RequestBody Rating rating, @RequestHeader String Authorization){
        if(paperService.evaluate(rating)>0)
            return BaseResult.success("评价成功");
        else
            return BaseResult.error();
    }

    /**
     * 用户（专家）查询审稿稿件列表
     */
    @ApiOperation("用户（专家）查询审稿稿件列表")
    @PostMapping("/paperList")
    @RequiresAuthentication
    public BaseResult<List<Paper>> list(@RequestBody ContentDto contentdto, @RequestHeader String Authorization)
    {
        return BaseResult.success("查询成功",paperService.paperList(contentdto));
    }

    /**
     * 用户查询自己的稿件
     */
    @ApiOperation("用户查询自己的稿件")
    @PostMapping("/paperListByMyself")
    @RequiresAuthentication
    public BaseResult<List<Paper>> paperListByMyself(@RequestBody ContentDto contentdto, @RequestHeader String Authorization)
    {

        return BaseResult.success("查询成功",paperService.paperListByMyself(contentdto,userUtil.getUserId()));
    }


    /**
     * 管理员查询审稿稿件列表
     */
    @RequiresRoles(value = {"管理员"})
    @ApiOperation("管理员查询征稿稿件列表")
    @PostMapping("/paperListByManager")
    public BaseResult<PaperByManagerResponse> paperListByManager(@RequestBody PaperRequest paperRequest, @RequestHeader String Authorization)
    {
        return BaseResult.success("查询成功",paperService.selectPaperList(paperRequest));
    }

    /**
     * 获取稿件详细信息
     */
    @RequiresAuthentication
    @ApiOperation("获取单个稿件详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<PaperDetailResponse> getInfo(@PathVariable("id") Long id, @RequestHeader String Authorization)
    {
        return BaseResult.success("查询成功",paperService.selectPaperById(id));
    }

    /**
     * 修改稿件
     */
    @RequiresAuthentication
    @ApiOperation("修改稿件")
    @PutMapping
    public BaseResult edit(@RequestBody Paper paper, @RequestHeader String Authorization)
    {
        if(paperService.updatePaper(paper)>0)
            return BaseResult.success("修改成功");
        else
            return BaseResult.error();
    }

    /**
     * 管理员删除稿件
     */
    @RequiresRoles(value = {"管理员"})
    @ApiOperation("管理员删除稿件")
    @DeleteMapping("/{ids}")
    public BaseResult remove(@PathVariable Long[] ids, @RequestHeader String Authorization)
    {
        if(paperService.deletePaperByIds(ids)>0)
            return BaseResult.success("删除成功");
        else
            return BaseResult.error();

    }

    /**
     * 删除稿件
     */
    @RequiresAuthentication
    @ApiOperation("用户删除自己的稿件")
    @GetMapping("/removeByMyselef")
    public BaseResult remove(Long id, @RequestHeader String Authorization)
    {
        if(paperService.deletePaperById(id)>0)
            return BaseResult.success("删除成功");
        else
            return BaseResult.error();
    }
}
